package login.processor;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.module.user.ProtoMessageLogin.C2SLogin;
import lion.common.MsgCodeAnn;
import lion.netty4.codec.ProtoUtil;
import lion.netty4.message.GamePlayer;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lion.netty4.processor.MsgProcessor;
import login.logic.LoginAccountManager;
import login.stat.LoginStat;

@MsgCodeAnn(msgcode = 1001, accessLimit = 200)
public class LoginProcessor extends MsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(LoginProcessor.class);

	private LoginAccountManager loginAccountManager = LoginAccountManager.getInstance();

	@Override
	public void process(GamePlayer player, RequestMessageRaw request) throws Exception {
		lua.LuaMessageLogin.C2SLogin reqMsg = lua.LuaMessageLogin.C2SLogin.parse(request);
		String userName = reqMsg.uname;
		logger.info("1001-login,userName={}", userName, player.getAddress());
		loginAccountManager.loginRaw(player, userName,"1");
	}

	@Override
	public void process(GamePlayer player, RequestProtoMessage request) throws Exception {
		C2SLogin c2sLogin = ProtoUtil.getProtoObj(C2SLogin.parser(), request);
		String deviceId = c2sLogin.getDeviceId();
		String userName = c2sLogin.getUserName();
		String pwd = c2sLogin.getPwd();
		String loginIp = c2sLogin.getLoginIp();
		LoginStat.getInstance().saveLoginTime(userName);
		logger.info("10013-login,deviceId={},userName={},pwd={},loginIp={}", deviceId, userName, pwd, loginIp);
		loginAccountManager.login(player, deviceId, userName, pwd, loginIp);
	}

	@Override
	public void process(GamePlayer player, MyRequestMessage request) throws Exception {
		String userName = request.readString();
		String pwd = request.readString();
		logger.info("10013-login,userName={},pwd={},loginIp={}", userName, pwd, player.getAddress());
		loginAccountManager.loginRaw(player, userName, pwd);
	}

}
